package com.hwxt.ghs.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hwxt.ghs.service.IndexService;
import com.hwxt.ghs.service.ProjectService;
import com.hwxt.ghs.vo.Project;

@Controller
public class CommitHistoryController {

    @Value("${lucene_directory}")
    private String lucene_directory;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private IndexService indexService;

    @GetMapping({"/","/index"})
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("lucene_directory", lucene_directory);
        List<Project> projectList = projectService.getProjectList();
        modelAndView.addObject("projects", projectList);
        int options[] = new int[]{ 100,200,500,1000};
        modelAndView.addObject("max",100);
        modelAndView.addObject("options",options);
        modelAndView.setViewName("index");
        return modelAndView;
    }
    // 跳转到新增项目页
    @GetMapping("/gotoAdd")
    public ModelAndView gotoAdd() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("add");
        return modelAndView;
    }
    // 保存增加的项目路径和名称信息
    @PostMapping("/addProject")
    public String addProject(Project project){
        projectService.addProject(project);
        // 重定向首页
        return "redirect:/";

    }
    // 删除项目
    @GetMapping("/deleteProject")
    public String deleteProject(String path){
        projectService.deleteProject(path);
        // 重定向首页
        return "redirect:/";
    }
    // 创建索引
    @GetMapping("/createIndex")
    public String createIndex(){
        List<Project> projectList = projectService.getProjectList();
        indexService.buildIndex(projectList);
        projectService.saveProjects(projectList);
        // 重定向到首页
        return "redirect:/";
    }

    @GetMapping("/search") 
    public ModelAndView search(Integer max,String query){
        if(max==null){
            max = 100;
        }
        List<Project> projectList = projectService.getProjectList();
        long begin = System.currentTimeMillis();
        List<Map<String,Object>> results = indexService.search(max,query);
        long end = System.currentTimeMillis();
        ModelAndView modelAndView = new ModelAndView();
        int options[] = new int[]{ 100,200,500,1000};
        modelAndView.addObject("query",query);
        modelAndView.addObject("max",max);
        modelAndView.addObject("options",options);
        modelAndView.addObject("lucene_directory", lucene_directory);
        modelAndView.addObject("projects", projectList);
        modelAndView.addObject("results",results);
        modelAndView.addObject("timecost",(end-begin));
        modelAndView.setViewName("index");
        return modelAndView;
    }
}